


function switch_options(option,id, target) 
{
	var piece = $("#form_type_options_" + option + "" + id).html();
	$('#' + target + id).hide().empty().html(piece).show('fast');	
}


function switch_link(target,id, link) 
{
	var text =  $(link).html();
	var val = text.indexOf('More') > -1;
	
	if(val) {
		$('#' + target + id).show('fast');
		$(link).html('Less');
		$(link).removeClass('more').addClass('less');
	} else {
		$('#' + target + id).hide('fast');
		$(link).html('More');
		$(link).removeClass('less').addClass('more');
	}
	
}


function conditional_display(target,value, comp_value, revert){
	if(!revert) {
		if(value == comp_value){
			$('#' + target ).hide('fast');
		} else {
			$('#' + target ).show('fast');
		}
	} else {
		if(value == comp_value){
			$('#' + target ).show('fast');
		} else {
			$('#' + target ).hide('fast');
		}
	}
}


function displaypreview(id, show, sticky){
	$("#preview_screen").slideToggle('fast');	
}   


function field_form_populate(count) {

	var sdata = $("#field_data" + count).val().replace(/'/g ,"\"");
	
	var data = $.parseJSON(sdata);

	set_field_value("label","text",count,data.label);
	set_field_value("name","text",count,data.name);
	set_field_value("id","text",count,data.id);
	set_field_value("class","text",count,data.class);
	set_field_value("type","combobox",count,data.type);
	
	set_field_value("type_input_size","combobox",count,data.type_input_size);
	set_field_value("type_input_datatype","combobox",count,data.type_input_datatype);
	set_field_value("type_input_datatype_regex","text",count,data.type_input_datatype_regex);
	
	set_field_value("type_textarea_maxsize","text",count,data.type_textarea_maxsize);
	set_field_value("type_textarea_rows","combobox",count,data.type_textarea_rows);
	set_field_value("type_textarea_columns","combobox",count,data.type_textarea_columns);

	set_field_value("type_dropdown_rows","combobox",count,data.type_dropdown_rows);
	set_field_value("type_dropdown_options","textarea",count,data.type_dropdown_options);
	set_field_value("type_dropdown_multiselect","checkbox",count,data.type_dropdown_multiselect);

	set_field_value("type_radio_layout","combobox",count,data.type_radio_layout);
	set_field_value("type_radio_options","textarea",count,data.type_radio_options);

	set_field_value("type_checkboxgroup_layout","combobox",count,data.type_checkboxgroup_layout);
	set_field_value("type_checkboxgroup_options","textarea",count,data.type_checkboxgroup_options);

	set_field_value("type_paragraph_layout","combobox",count,data.type_paragraph_layout);
	set_field_value("type_paragraph_layout_boxed_rows","combobox",count,data.type_paragraph_layout_boxed_rows);
	set_field_value("type_paragraph_text","textarea",count,data.type_paragraph_text);

	set_field_value("validation_required","checkbox",count,data.validation_required);
	set_field_value("validation_trim","checkbox",count,data.validation_trim);
	set_field_value("validation_xssclean","checkbox",count,data.validation_xssclean);
	
	
	set_field_value("footnote","textarea","view_field_footnote");
	set_field_value("label_footnote","textarea","view_field_label_footnote");
	set_field_value("db_field_type","combobox","db_field_type");
	set_field_value("db_field_length_value","text","db_field_length_value");
	set_field_value("db_field_length_default","text","db_field_length_default");
	
	
}


function set_field_value(name,type,count,value) {

console.log("name:" + name + ", type:" + type + " count:" + count + " value:" + value );

	var element = $("[name=view_field_" + name + count +"]");

	
	if(type == "text"){
		element.val(value);
	}
	
	if(type == "textarea"){
		element.html(value);
	}	
	
	if(type == "combobox"){
		element.val(value);
	}	
	
	if(type == "checkbox"){
		element.checked = value;
	}	
			
}




function field_form_compute() {

/*
"label"									: "label abc",
"name"									: "name abc",
"id"									: "",
"class"									:"",
"type"				 					: "input",

"type_input_size"						: "1",
"type_input_datatype"					: "numeric",
"type_input_datatype_regex" 			: "",

"type_textarea_maxsize"					: "",
"type_textarea_rows"					: "1",
"type_textarea_columns"					: "1",

"type_dropdown_rows" 					: "1",
"type_dropdown_options" 				: "",
"type_dropdown_multiselect" 			: false,

"type_radio_layout" 					: "horizontal",
"type_radio_options" 					: "",

"type_checkboxgroup_layout"				: "horizontal",
"type_checkboxgroup_options"			: "",

"type_paragraph_layout"					: "simple",
"type_paragraph_layout_boxed_rows"		: "1",
"type_paragraph_text"					: "horizontal",

"validation_required" 					: true,
"validation_trim"						: false,
"validation_xssclean"					: true,										
*/

var data = {
	  "label" : get_field_value("label","text",count)
	, "name" : get_field_value("name","text",count)
	, "id" : get_field_value("id","text",count)
	, "class" : get_field_value("class","text",count)
	, "type" : get_field_value("type","combobox",count)
	
	, "type_input_size" : get_field_value("type_input_size","combobox",count)
	, "type_input_datatype" : get_field_value("type_input_datatype","combobox",count)
	, "type_input_datatype_regex" : get_field_value("type_input_datatype_regex","text",count)

	, "type_textarea_maxsize" : get_field_value("type_textarea_maxsize","text",count)
	, "type_textarea_rows" : get_field_value("type_textarea_rows","combobox",count)
	, "type_textarea_columns" : get_field_value("type_textarea_columns","combobox",count)

	, "type_dropdown_rows" : get_field_value("type_dropdown_rows","combobox",count)
	, "type_dropdown_options" : get_field_value("type_dropdown_options","textarea",count)
	, "type_dropdown_multiselect" : get_field_value("type_dropdown_multiselect","checkbox",count)
	
	, "type_radio_layout" : get_field_value("type_radio_layout","combobox",count)
	, "type_radio_options" : get_field_value("type_radio_options","textarea",count)

	, "type_checkboxgroup_layout" : get_field_value("type_checkboxgroup_layout","combobox",count)
	, "type_checkboxgroup_options" : get_field_value("type_checkboxgroup_options","textarea",count)

	, "type_paragraph_layout" : get_field_value("type_paragraph_layout","combobox",count)
	, "type_paragraph_layout_boxed_rows" : get_field_value("type_paragraph_layout_boxed_rows","combobox",count)
	, "type_paragraph_text" : get_field_value("type_paragraph_text","textarea",count)

	, "validation_required" : get_field_value("validation_required","checkbox",count)
	, "validation_trim" : get_field_value("validation_trim","checkbox",count)
	, "validation_xssclean" : get_field_value("validation_xssclean","checkbox",count)	
};

alert(data);

$("#field_data").val(data);

}



function get_field_value(name,type,count) {

	var element = $("[name=view_field_" + name + count +"]");
	
	if(element == null || element.val() == null) {
		alert("The element #view_field_" + name + count + " do not have a value ." + element );
	}
	
	if(type == "text"){
		return element.val();
	}
	
	if(type == "textarea"){
		return element.html();
	}	
	
	if(type == "combobox"){
		return element.val();
	}	
	
	if(type == "checkbox"){
		return element.checked;
	}	
			
}